package com.hhkj.study.common.handler;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Maps;
import com.ruoyi.common.constant.Constants;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author luowu
 * @Date 2024/9/11 涵 8:48:28
 */
public class CommonHandler {

    /**
     * 获取map中值并用设置好的分割符拼接
     * @param map
     * @param list
     * @param split
     * @return
     */
    public static String joinStr(Map<Long, String> map, List<Long> list, String split){
        if (CollectionUtil.isEmpty(list) || CollectionUtil.isEmpty(map)) {
            return null;
        }
        String joinStr = list.stream().map(e -> map.getOrDefault(e, "")).collect(Collectors.joining(split));
        return StrUtil.removeSuffix(joinStr, Constants.VERTICAL_LINE);
    }

    /**
     * 通过字典map组装成map结合key-value格式
     * @param dictMap
     * @param idList
     * @return
     */
    public static List<Map<String, Object>> getMapByDict(Map<Long, String> dictMap, List<Long> idList){
        if (CollectionUtil.isEmpty(idList) || CollectionUtil.isEmpty(dictMap)) {
            return null;
        }

        List<Map<String, Object>> mapList = idList.stream().map(id -> {
            Map<String, Object> map = null;
            String value = dictMap.get(id);
            if (StrUtil.isNotBlank(value)) {
                map = Maps.newHashMap();
                map.put("id", id);
                map.put("value", value);
            }
            return map;
        }).collect(Collectors.toList());
        mapList.removeIf(Objects::isNull);
        return mapList;
    }
}
